package com.help10000.rms.ui.activitys.logins

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import com.amir.common.utils.HideUtil
import com.amir.common.utils.IMEUtil
import com.amir.common.utils.ToastUtil
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.ForgetPwdPresenter
import com.help10000.rms.mvp.view.ForgetPwdView
import com.help10000.rms.ui.base.BaseActivity
import com.help10000.rms.ui.utils.PageUtils
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.activity_forget_pwd.btn_submit
import kotlinx.android.synthetic.main.activity_forget_pwd.et_affpwd
import kotlinx.android.synthetic.main.activity_forget_pwd.et_phone
import kotlinx.android.synthetic.main.activity_forget_pwd.et_pwd
import kotlinx.android.synthetic.main.activity_forget_pwd.et_smscode
import kotlinx.android.synthetic.main.activity_forget_pwd.resetPwdGroup
import kotlinx.android.synthetic.main.activity_forget_pwd.secondTitle
import kotlinx.android.synthetic.main.activity_forget_pwd.tv_send
import kotlinx.android.synthetic.main.activity_forget_pwd.tv_time
import kotlinx.android.synthetic.main.activity_forget_pwd.validatePhoneGroup


/**
 * 忘记密码
 */
class ForgetPwdActivity : BaseActivity<ForgetPwdPresenter, ForgetPwdView>(), ForgetPwdView,
    View.OnClickListener {

    override fun getlayoutId(): Int {
        return R.layout.activity_forget_pwd
    }

    override fun initView() {


    }

    override fun initData(savedInstanceState: Bundle?) {
        et_phone.requestFocus()
    }

    override fun initListener() {
        super.initListener()

        tv_send.setOnClickListener(DuplicateClickProxy(this));
        btn_submit.setOnClickListener(DuplicateClickProxy(this));

        HideUtil.openKeyboard(this, et_phone)
    }

    override fun onDestroy() {
        super.onDestroy()
        mCountDownTimer?.cancel()
    }

    var smsSend = false
    var mCountDownTimer: CountDownTimer? = null
    override fun sendSmsSuccess() {
        smsSend = true
        tv_time.visibility = View.VISIBLE
        mCountDownTimer = object : CountDownTimer(30 * 1000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                tv_time.text = "剩余${millisUntilFinished / 1000}s"
            }

            override fun onFinish() {
                tv_time.visibility = View.GONE
                tv_time.text = "剩余30s"
                tv_send.text = "重新发送"
            }
        }.start()
        HideUtil.openKeyboard(this, et_smscode)
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_send -> {
                var phone = et_phone.text.toString();
                var reg = Regex("1[3-9][0-9]{9}")
                if (!reg.matches(phone)) {
                    ToastUtil.show("手机号码输入有误")
                    return
                }
                HideUtil.hideSoftKeyboard(et_phone)
                mPresenter.sendSms(et_phone.text.toString())
            }
            R.id.btn_submit -> {
                if (validatePhoneGroup.visibility == View.VISIBLE) {
                    var phone = et_phone.text.toString();
                    var reg = Regex("1[3-9][0-9]{9}")
                    if (!reg.matches(phone)) {
                        ToastUtil.show("手机号码输入有误")
                        return
                    }
                    if (!smsSend) {
                        ToastUtil.show("请先发送验证码")
                        return
                    }
                    if (et_smscode.text.toString().length < 4) {
                        ToastUtil.show("请输入正确的验证码")
                        return
                    }
                    IMEUtil.hideIME(this, et_phone)
                    mPresenter.checkSms(et_phone.text.toString(), et_smscode.text.toString())
                } else {
                    IMEUtil.hideIME(this, et_pwd)
                    mPresenter.forgetPwd(
                        et_phone.text.toString(),
                        et_pwd.text.toString(),
                        et_affpwd.text.toString(),
                        et_smscode.text.toString()
                    )
                }
            }
        }
    }

    override fun createPresenter(): ForgetPwdPresenter {
        return ForgetPwdPresenter(this)
    }

    override fun checkSMSSuccess() {
        secondTitle.setText(R.string.reset_pwd_title)
        validatePhoneGroup.visibility = View.GONE
        resetPwdGroup.visibility = View.VISIBLE
        btn_submit.setText(R.string.confirm_tip)
    }

    override fun changeSucc() {
        XPopup.Builder(this)
            .asConfirm(
                null, "密码重置成功", null, "去登录", {
                    PageUtils.jumpToActivity(this, LoginActivity::class.java)
                }, null, true
            ).show()
    }

    override fun loginSuccess() {
        TODO("Not yet implemented")
    }
}
